package com.sgcc.sx.pmos.e_sx_order.fragment;

import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sgcc.sx.pmos.arouter.app.AppArouterPath;
import com.sgcc.sx.pmos.arouter.mall.MallArouterPath;
import com.sgcc.sx.pmos.arouter.order.OrderArouterPath;
import com.sgcc.sx.pmos.common.base.MvpBaseRecycleFragment;
import com.sgcc.sx.pmos.common.bean.BaseResponse;
import com.sgcc.sx.pmos.common.bean.EventCenter;
import com.sgcc.sx.pmos.common.bean.IndentOrContractBean;
import com.sgcc.sx.pmos.common.utils.ArguConstant;
import com.sgcc.sx.pmos.common.utils.UserManager;
import com.sgcc.pmos.nativesdk.R;
import com.sgcc.sx.pmos.e_sx_order.adapter.ContractAdapter;
import com.sgcc.sx.pmos.e_sx_order.bean.IndentBean;
import com.sgcc.sx.pmos.e_sx_order.contract.ContractContract;
import com.sgcc.sx.pmos.e_sx_order.presenter.ContractPresenter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * 合约
 * <p>
 * 下边注释的东西不要删除 因接口不支持 暂时去掉
 */
public class ContractFragment extends MvpBaseRecycleFragment<IndentOrContractBean, ContractPresenter> implements ContractContract.View {

    private ContractAdapter mContractAdapter;

//    private RelativeLayout mRlAll;
//    private TextView mTvAll;
//    private RelativeLayout mRlBgz;
//    private TextView mTvBgz;
//    private RelativeLayout mRlYsx;
//    private TextView mTvYsx;
//    private RelativeLayout mRlYjy;
//    private TextView mTvYjy;
//    private RelativeLayout mRlDpj;
//    private TextView mTvDpj;

    @Override
    public void querySuccess(IndentBean response) {
        if (response != null) {
            if (response.getCode() == 200) {
                if (response.getData() != null) {
                    loadSuccess(response.getData().isHasNextPage(), response.getData().getList());
                }
            } else {

                loadFalied();
            }
        }
    }

    @Override
    public void queryFailed() {
        loadFalied();
    }

    @Override
    public void relieveContractSuccess(BaseResponse response) {
        if (response != null) {
            if (response.isSuccess()) {
                refresh(false);
            }
        }
    }

    @Override
    public void relieveContractFailed() {

    }

    @Override
    public void onResume() {
        super.onResume();
        onChangePage();
    }

    @Override
    public boolean isBindEventBusHere() {
        return true;
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(EventCenter eventCenter) {
        if (eventCenter.getEventCode() == ArguConstant.LOGOUT) {
            onChangePage();
        } else if (eventCenter.getEventCode() == ArguConstant.REFRESH_CONTRACT) {
            refresh(false);
        } else if (eventCenter.getEventCode() == ArguConstant.INTERFACE_LOGOUT) {
            onChangePage();
        }
    }


    private void onChangePage() {
        if (UserManager.isLogin()) {
            ll_content.setVisibility(View.VISIBLE);
            not_login_layout.setVisibility(View.GONE);
            mPresenter.getContractList(0, false);
        } else {
            View notLoginView = getLayoutInflater().inflate(R.layout.not_login_contract_layout, not_login_layout, false);
            TextView mTvLogin = (TextView) notLoginView.findViewById(R.id.tv_login);
            mTvLogin.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    ARouter.getInstance()
                            .build(AppArouterPath.APP_LOGIN)
                            .navigation();
                }
            });
            not_login_layout.addView(notLoginView);
            not_login_layout.setVisibility(View.VISIBLE);
            ll_content.setVisibility(View.GONE);
        }
    }

    @Override
    protected ContractPresenter createPresenter() {
        return new ContractPresenter();
    }

    @Override
    public View getHeaderLayout(RelativeLayout parent) {
        View head_view = getLayoutInflater().inflate(R.layout.header_contract_new_layout, parent, false);
        return head_view;
    }

//    @Override
//    public View getHeaderLayout(RelativeLayout parent) {
//        View head_view = getLayoutInflater().inflate(R.layout.header_contract_layout, parent, false);
//        findViewByIds(head_view);
//        return head_view;
//    }
//
//    private void findViewByIds(View head_view) {
//        mRlAll = (RelativeLayout) head_view.findViewById(R.id.rl_all);
//        mTvAll = (TextView) head_view.findViewById(R.id.tv_all);
//        mRlBgz = (RelativeLayout) head_view.findViewById(R.id.rl_bgz);
//        mTvBgz = (TextView) head_view.findViewById(R.id.tv_bgz);
//        mRlYsx = (RelativeLayout) head_view.findViewById(R.id.rl_ysx);
//        mTvYsx = (TextView) head_view.findViewById(R.id.tv_ysx);
//        mRlYjy = (RelativeLayout) head_view.findViewById(R.id.rl_yjy);
//        mTvYjy = (TextView) head_view.findViewById(R.id.tv_yjy);
//        mRlDpj = (RelativeLayout) head_view.findViewById(R.id.rl_dpj);
//        mTvDpj = (TextView) head_view.findViewById(R.id.tv_dpj);
//
//        mRlAll.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                mTvAll.setTextColor(Color.parseColor("#FF0092FF"));
//                mTvBgz.setTextColor(Color.parseColor("#FF999999"));
//                mTvYsx.setTextColor(Color.parseColor("#FF999999"));
//                mTvYjy.setTextColor(Color.parseColor("#FF999999"));
//                mTvDpj.setTextColor(Color.parseColor("#FF999999"));
//            }
//        });
//        mRlBgz.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                mTvAll.setTextColor(Color.parseColor("#FF999999"));
//                mTvBgz.setTextColor(Color.parseColor("#FF0092FF"));
//                mTvYsx.setTextColor(Color.parseColor("#FF999999"));
//                mTvYjy.setTextColor(Color.parseColor("#FF999999"));
//                mTvDpj.setTextColor(Color.parseColor("#FF999999"));
//            }
//        });
//        mRlYsx.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                mTvAll.setTextColor(Color.parseColor("#FF999999"));
//                mTvBgz.setTextColor(Color.parseColor("#FF999999"));
//                mTvYsx.setTextColor(Color.parseColor("#FF0092FF"));
//                mTvYjy.setTextColor(Color.parseColor("#FF999999"));
//                mTvDpj.setTextColor(Color.parseColor("#FF999999"));
//            }
//        });
//        mRlYjy.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                mTvAll.setTextColor(Color.parseColor("#FF999999"));
//                mTvBgz.setTextColor(Color.parseColor("#FF999999"));
//                mTvYsx.setTextColor(Color.parseColor("#FF999999"));
//                mTvYjy.setTextColor(Color.parseColor("#FF0092FF"));
//                mTvDpj.setTextColor(Color.parseColor("#FF999999"));
//            }
//        });
//        mRlDpj.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                mTvAll.setTextColor(Color.parseColor("#FF999999"));
//                mTvBgz.setTextColor(Color.parseColor("#FF999999"));
//                mTvYsx.setTextColor(Color.parseColor("#FF999999"));
//                mTvYjy.setTextColor(Color.parseColor("#FF999999"));
//                mTvDpj.setTextColor(Color.parseColor("#FF0092FF"));
//            }
//        });
//    }

    @Override
    public BaseQuickAdapter getAdapter() {
        mContractAdapter = new ContractAdapter(mContext);
        mContractAdapter.setItemClickListener(new ContractAdapter.OnItemClickListener() {

            @Override
            public void OnCompanyNameClickListener(int position, IndentOrContractBean bean) {
                ARouter.getInstance()
                        .build(MallArouterPath.MALL_STOREHOMEPAGE)
                        .withString("membersSdId", bean.getMembersSdId())
                        .navigation();
            }

            @Override
            public void OnLookinfoClickListener(int position, IndentOrContractBean bean) {
                ARouter.getInstance()
                        .build(OrderArouterPath.ORDER_ORDERDETAILS)
                        .withString("relCode", bean.getRelCode())
                        .withString("from", "2")
                        .withString("updateDate", bean.getUpdateDate())
                        .navigation();
            }

            @Override
            public void OnTerminationClickListener(int position, IndentOrContractBean bean) {
                mPresenter.relieveContract(bean.getRelationId());
            }

            @Override
            public void OnImmediateEvaluationClickListener(int position, IndentOrContractBean bean) {
                //TODO 立即评价
                ToastUtils.showShort("该功能正在开发");
            }

            @Override
            public void OnAgainBuyClickListener(int position, IndentOrContractBean bean) {
                EventBus.getDefault().post(new EventCenter<>(ArguConstant.GO_SIGNING));
            }
        });
        return mContractAdapter;
    }

    @Override
    public void getDataRequest(int listSize, boolean isloading) {
        super.getDataRequest(listSize, isloading);
        if (UserManager.isLogin()) {
            mPresenter.getContractList(listSize, isloading);
        }
    }
}